# vim: ts=4:sw=4:syntax=perl

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use File::Basename;
use xml_helper;
use Data::Dumper;

sub mm_form_modify_portal_process {
my $_aa=new xml_helper();

	my $str="";

	my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
	my $portal=$::PARAM{portal};
	my $portal_title=$::PARAM{title};
	my $portal_filename=$::PARAM{filename};
	my $portal_users=$::PARAM{users};
	my $portal_owners=$::PARAM{owners};
	my $portal_voters=$::PARAM{voters};
	my $portal_concur=$::PARAM{concur};
	my $portal_template=$::PARAM{template};

	$portal_filename="" if !defined($portal_filename);
	$portal_users="" if !defined($portal_users);
	$portal_owners="" if !defined($portal_owners);
	$portal_voters="" if !defined($portal_voters);
	$portal_concur="" if !defined($portal_concur);
	$portal_template="" if !defined($portal_template);
	$portal_filename =~ s/\s+//g; $portal_filename=lc(basename($portal_filename));

	#################################################################
	# This is the modify option - so we don't care if the specified
	# portal already exists.
	# name and raise an error if we do.				#
	#################################################################
	{
		my $serror="";
		my $error;

		my $x=$::GRI_FRONTEND->get_global_directory("portals");
		if(!length($portal_filename) ) {
			$serror="Error Adding New Portal";
			$error="Required portal filename not specified.";
		} elsif(! -f "$x/$portal_filename") {
			$serror="Error Modifying Existing Portal";
			$error="Specified portal filename to modify does not exist: $x/$portal_filename.";
		}
		if(defined($error)) {
			my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
			if(!exists($::PARAM{lp})) {
				# We need to encode the current data for repopulation into the form!
				my $s="";
				$s.="portal=$portal";
				$s.="::portal_title=".string_to_hex($portal_title);
				$s.="::portal_filename=".string_to_hex($portal_filename);
				$s.="::portal_users=".string_to_hex($portal_users);
				$s.="::portal_voters=".string_to_hex($portal_voters);
				$s.="::portal_owners=".string_to_hex($portal_owners);
				$s.="::portal_concur=".string_to_hex($portal_concur);
				$s.="::portal_template=".string_to_hex($portal_template);
				$link.=",previouspage=form_modify_portal::$s";
			}
			redirect_header(path => $link);
			exit;
		}
	}

	#################################################################################
	# If users have been specified then remove all the white space and send		#
	# across to the backend to validate whether the users exist, and if not we	#
	# issue an error message.							#
	#################################################################################

	my $serror=$error=undef;
	if($portal_users) {
		$rc=$::GRI_FRONTEND->query_authd("VALID_USERS USERS=$portal_users");
		if(!defined($rc)) {
			$serror="Error Modifying Portal";
			$error="Unable to communicate with Authentication server.";
		}
		if(!defined($serror) && defined($rc) && $rc =~ /^FAIL/) {
			$rc=~ s/^FAIL\s+//;
			$serror="Error Modifying Portal";
			$error="The following users specified do not exist: $rc";
		}
	}
	if($portal_owners) {
		$rc=$::GRI_FRONTEND->query_authd("VALID_USERS USERS=$portal_owners");
		if(!defined($rc)) {
			$serror="Error Modifying Portal";
			$error="Unable to communicate with Authentication server.";
		}
		print STDERR "rc=$rc\n";
		if(!defined($serror) && defined($rc) && $rc =~ /^FAIL/) {
			$rc=~ s/^FAIL\s+//;
			$serror="Error Modifying Portal";
			$error="The following owners specified do not exist: $rc";
		}
	}
	if($portal_voters) {
		$rc=$::GRI_FRONTEND->query_authd("VALID_USERS USERS=$portal_voters");
		if(!defined($rc)) {
			$serror="Error Modifying Portal";
			$error="Unable to communicate with Authentication server.";
		}
		if(!defined($serror) && defined($rc) && $rc =~ /^FAIL/) {
			$rc=~ s/^FAIL\s+//;
			$serror="Error Modifying Portal";
			$error="The following voters specified do not exist: $rc";
		}
	}

	if(defined($serror)) {
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="portal_title=".string_to_hex($portal_title);
			$s.="::portal=$portal";
			$s.="::portal_filename=".string_to_hex($portal_filename);
			$s.="::portal_users=".string_to_hex($portal_users);
			$s.="::portal_voters=".string_to_hex($portal_voters);
			$s.="::portal_owners=".string_to_hex($portal_owners);
			$s.="::portal_concur=".string_to_hex($portal_concur);
			$s.="::portal_template=".string_to_hex($portal_template);
			$link.=",previouspage=form_modify_portal::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	my $ob=new_empty gri_portal_config;

	# add all the data to the empty object

	$ob->set_title($portal_title);
	$ob->set_filename($portal_filename);
	if(defined($portal_users) && length($portal_users)) {
		$ob->set_users($portal_users);
	}
	if(defined($portal_owners) && length($portal_owners)) {
		$ob->set_owners($portal_owners);
	}
	if(defined($portal_voters) && length($portal_voters)) {
		$ob->set_voters($portal_voters);
	}
	if(defined($portal_concur) && length($portal_concur)) {
		$ob->set_concur_count($portal_concur);
	}
	if(defined($portal_concur) && length($portal_concur)) {
		$ob->set_concur_count($portal_concur);
	}

	# Everything is good - so call routine to write the file
	my ($rc,$err)=$ob->write_file();
	if(!$rc) {
		my $error="Failed writing existing Portal file.";
		my $serror=$err;
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="portal=$portal";
			$s.="::portal_title=".string_to_hex($portal_title);
			$s.="::portal_filename=".string_to_hex($portal_filename);
			$s.="::portal_users=".string_to_hex($portal_users);
			$s.="::portal_voters=".string_to_hex($portal_voters);
			$s.="::portal_owners=".string_to_hex($portal_owners);
			$s.="::portal_concur=".string_to_hex($portal_concur);
			$s.="::portal_template=".string_to_hex($portal_template);
			$link.=",previouspage=form_modify_portal::$s";
		}
		redirect_header(path => $link);
		exit;
	}
	my $link="$url_prefix$::ENV{SCRIPT_NAME}/admin_page";
	redirect_header(path => $link);
	exit;
}

1;
